package my2Cents;

import java.awt.event.KeyEvent;
import java.util.Vector;
import net.phys2d.math.Vector2f;
import net.phys2d.raw.Body;
import net.phys2d.raw.shapes.PBox;
import net.phys2d.raw.shapes.PhysShape;

public strictfp class Player extends Body {
	private static PhysShape shape = new PBox(50,80);
	private static Vector<KeyEvent> keys = new Vector<KeyEvent>();
	
	public Player() {
		super(shape, 120);
		this.setRotatable(false);
	}
	
	public void keyPressed(KeyEvent arg0) {
		for(int i=0; i<keys.size(); i++) {
			KeyEvent key = keys.get(i);
			if(key.getKeyCode() == arg0.getKeyCode()) {
				return;
			}
		}
		System.out.println("KeyPress: " + arg0.getKeyChar());
		keys.add(arg0);
	}
	
	public void keyReleased(KeyEvent arg0) {
		for(int i=0; i<keys.size(); i++) {
			KeyEvent key = keys.get(i);
			if(key.getKeyCode() == arg0.getKeyCode()) {
				keys.remove(i);
			}
		}
	}
	
	public void think() {
		super.think();
		Vector2f vel = new Vector2f(0,0);
		vel.y = this.getVelocity().getY();
		vel.x = this.getVelocity().getX();
		for(int i=0; i<keys.size(); i++) {
			KeyEvent key = keys.get(i);
			if(key.getKeyChar() == 'd') vel.x = 25;
			if(key.getKeyChar() == 'a') vel.x = -25;
			if(key.getKeyChar() == 's') vel.y = 25;
			if(key.getKeyChar() == 'w') vel.y = -25;
		}
		if(Sandbox.isRunning() == false) {
			Vector2f pos = new Vector2f();
			vel.mul(0.3f);
			pos.add(vel);
			this.adjustPosition(pos);
		} else {
			this.setVelocity(vel);
		}
	}

}
